package com.shoe.service;

import com.shoe.mapper.ShoeLimitMapper;
import com.shoe.mapper.ShoeUserMapper;
import com.shoe.pojo.ShoeLimit;
import com.shoe.pojo.ShoeLimitExample;

import com.shoe.pojo.ShoeUser;
import com.shoe.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class LoginImpl implements ShoeLoginService {

    @Autowired
    ShoeLimitMapper shoeLimitMapper;

    @Autowired
    ShoeUserMapper shoeUserMapper;


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ShoeLimit selectLogin(String name, String pwd) {
        ShoeLimitExample shoeLimitExample = new ShoeLimitExample();
        shoeLimitExample.createCriteria().andUserNameEqualTo(name).andUserPasswordEqualTo(pwd);
        List<ShoeLimit> limitList = shoeLimitMapper.selectByExample(shoeLimitExample);
        if (null != limitList && limitList.size() > 0){
               return limitList.get(0);
        }
      return null;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int addUser(ShoeLimit shoeLimit) {
        int a = shoeLimitMapper.insert(shoeLimit);
        System.out.println("111111");

        ShoeUser shoeUser = new ShoeUser();
        shoeUser.setUserName(shoeLimit.getUserName());
        shoeUserMapper.insert(shoeUser);


        return a;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int update(ShoeLimit userPassword) {
        int a = shoeLimitMapper.updateByPrimaryKey(userPassword);
        return a;
    }

    @Override
    public ResponseResult selectLoginByRemember(String uname, String loginPwd) {

        ShoeLimitExample shoeLimitExample = new ShoeLimitExample();
        shoeLimitExample.createCriteria().andUserNameEqualTo(uname).andUserPasswordEqualTo(loginPwd);
        List<ShoeLimit> shoeLimitList = shoeLimitMapper.selectByExample(shoeLimitExample);
        ResponseResult responseResult = new ResponseResult();
        if (shoeLimitList != null && shoeLimitList.size() > 0){
            responseResult.setCode(1);
            responseResult.setMsg("登录成功");
            responseResult.setData(shoeLimitList);
        }else {
            responseResult.setCode(3);
        }
        return responseResult;
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ResponseResult selectUserName(String username) {
        ShoeLimitExample shoeLimitExample = new ShoeLimitExample();
        shoeLimitExample.createCriteria().andUserNameEqualTo(username);
        List<ShoeLimit> limitList = shoeLimitMapper.selectByExample(shoeLimitExample);
        ResponseResult  responseResult = new ResponseResult();
        if (limitList != null && limitList.size() > 0){
            responseResult.setCode(2);
        }else {
            responseResult.setCode(1);
        }
        return responseResult;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ResponseResult selectPassWord(String userpassWord) {
        ShoeLimitExample shoeLimitExample = new ShoeLimitExample();
        shoeLimitExample.createCriteria().andUserPasswordEqualTo(userpassWord);
        List<ShoeLimit> limitList = shoeLimitMapper.selectByExample(shoeLimitExample);
        ResponseResult responseResult = new ResponseResult();
        if (limitList != null && limitList.size() >0){
            responseResult.setCode(2);
        }else {
            responseResult.setCode(1);
        }
        return responseResult;
    }
}
